package cn.stylefeng.guns.modular.rest;


import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.modular.comm.entity.News;
import cn.stylefeng.guns.modular.comm.model.params.NewsParam;
import cn.stylefeng.guns.modular.comm.model.result.NewsResult;
import cn.stylefeng.guns.modular.comm.service.NewsService;
import cn.stylefeng.guns.modular.usr.model.params.FavorParam;
import cn.stylefeng.guns.modular.usr.model.result.FavorResult;
import cn.stylefeng.guns.modular.usr.service.FavorService;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 轮播图控制器
 *
 * @author
 * @Date 2019-11-18 13:24:11
 */
@RestController
@RequestMapping("/miniprogramApi/news")
public class ApiNewsController extends BaseController {
    private final NewsService newsService;
    private final GunsProperties gunsProperties;
    private final FavorService favorService;

    public ApiNewsController(NewsService newsService, GunsProperties gunsProperties, FavorService favorService) {
        this.newsService = newsService;
        this.gunsProperties = gunsProperties;
        this.favorService = favorService;
    }

    /**
     * 首页
     */
    @RequestMapping(value = "/detail")
    public Object detail(int id, String openId) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        // 收藏
        FavorParam favorParam = new FavorParam();
        favorParam.setOpenId(openId);
        favorParam.setNewsId(id);
        FavorResult favorResult = favorService.findBySpec(favorParam);
        if (favorResult.getId() != null) {
            // 已收藏
            map.put("favor", "yes");
        } else {
            // 未收藏
            map.put("favor", "no");
        }

        // 新闻
        NewsResult newsResult = new NewsResult();
        News news = newsService.getById(id);
        ToolUtil.copyProperties(news, newsResult);
        newsResult.setInsertTimeStr(DateUtil.format(newsResult.getInsertTime(), "yyyy-MM-dd HH:mm"));
        map.put("news", newsResult);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }

    /**
     * 首页
     */
    @RequestMapping(value = "/list")
    public Object list(NewsParam newsParam) {
        Map map = new HashMap();
        SuccessResponseData SUCCESS_TIP = new SuccessResponseData();

        // 新闻
        List<NewsResult> newsResults = newsService.findNews(newsParam);
        newsResults.forEach(newsResult -> {
            if (newsResult.getFilePath() != null) {
                String substring = newsResult.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1).replaceAll("\\\\", "/");
                newsResult.setFilePath(substring);
            }
            String contentText = "";
            if (newsResult.getContentText() != null && newsResult.getContentText().replaceAll("\n", "").replaceAll("　", "").replaceAll(" ", "").trim().length() > 20) {
                contentText = newsResult.getContentText().replaceAll("\n", "").replaceAll("　", "").replaceAll(" ", "").trim().substring(0, 20);
            } else {
                contentText = newsResult.getContentText().replaceAll("\n", "").replaceAll("　", "").replaceAll(" ", "").trim();
            }
            newsResult.setContentText(contentText);
        });
        map.put("news", newsResults);

        SUCCESS_TIP.setData(map);
        return SUCCESS_TIP;
    }
}


